package com.baymax.test.scene.demo

import android.animation.Animator
import android.animation.Keyframe
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.animation.TypeEvaluator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.GradientDrawable
import android.util.Log
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.LinearInterpolator
import android.widget.LinearLayout

class WKGroup(context: Context) : View(context) {

    val w = 180
    val h = 80
    val deco = 10

    val drawable = GradientDrawable().apply {
        setBounds(15, 10, 180, 70)
        setColor(Color.YELLOW)
        cornerRadius = 60f
        callback = this@WKGroup

    }

    override fun onDraw(canvas: Canvas) {

        drawable.draw(canvas)
        super.onDraw(canvas)
        repeat(20) {
            drawText(canvas, it)
        }
    }

    val dotPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        setColor(Color.RED)
    }
    val txtPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        setColor(Color.BLUE)
        textSize = 32f
    }

    private fun drawText(canvas: Canvas, i: Int) {
        val l = 15
        val top = (i + 1) * deco + i * h
        val centerY = top.toFloat() + h / 2f
        canvas.drawCircle(8f, centerY, 4f, dotPaint)
        val string = "标题 $i"
        canvas.drawRoundRect(30f, centerY - 15f, 200f, centerY + 15f, 12f, 12f, txtPaint)
    }


    val rectEvaluator = object : TypeEvaluator<Rect> {
        val r = Rect()
        override fun evaluate(
            f: Float,
            startValue: Rect,
            endValue: Rect
        ): Rect {
            r.set(this@WKGroup.evaluate(f, startValue, endValue))
            Log.i("WkGroup", "evaluate: $f , $r , s=$startValue ,e=$endValue")
            drawable.bounds = r
            invalidate()
            return r

        }
    }

    private fun evaluate(f: Float, startValue: Rect, endValue: Rect): Rect {
        val left = startValue.left + ((endValue.left - startValue.left) * f)
        val right = startValue.right + ((endValue.right - startValue.right) * f)
        val top = startValue.top + ((endValue.top - startValue.top) * f)
        val bottom = startValue.bottom + ((endValue.bottom - startValue.bottom) * f)

        return Rect(left.toInt(), top.toInt(), right.toInt(), bottom.toInt()).apply {


        }
    }

    fun Rect.insetOf(left: Int, top: Int, right: Int, bottom: Int) {
        this.left += left;
        this.top += top;
        this.right -= right;
        this.bottom -= bottom;
    }

    var animator: Animator? = null
    fun moveTo(rect: Rect) {
        animator?.cancel()
        val st = Rect(drawable.bounds)
        val up = rect.top <= st.top
        val per50 = rect.height() / 2
        val p80 = Rect(rect).apply {
            if (up) {
                offset(0, per50 / 5)
                insetOf(0, 0, 0, -per50 / 3)
            } else {
                offset(0, -per50 / 5)
                insetOf(0, -per50 / 3, 0, 0)
            }
        }


        val holder = PropertyValuesHolder.ofKeyframe(
            "rect",
            Keyframe.ofObject(0f, st), //t
            Keyframe.ofObject(0.80f, Rect(rect)),
            Keyframe.ofObject(0.92f, p80),//r
            Keyframe.ofObject(1f, Rect(rect)),
        ).apply {
            setEvaluator(rectEvaluator)
        }
        Log.d("WKGroup", "moveTo: holder $holder")
        animator = ObjectAnimator.ofPropertyValuesHolder(drawable.bounds, holder)
            .apply {
                setDuration(400)
                interpolator = AccelerateDecelerateInterpolator()
            }
        Log.d("WKGroup", "move to ${drawable.bounds} -> $rect")
        animator?.start()
    }

}